<?php

namespace app\api\module;

use app\common\model\AppVersion;
use app\common\model\AppVersionIos;
use app\common\model\LoanCal;
use app\common\model\Region;
use app\common\model\Settings;
use app\common\model\Sms;
use app\common\model\Utils as UtilsModel;
use think\facade\Db;

class Utils extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        //不需登录的接口
        $ignoreList = [
            WJH_UTILS_SETTINGS,
            ZFN_UTILS_SMS_CODE,
            ZFN_UTILS_APP_VERSION,
            ZFN_UTILS_APP_VERSION_IOS,
            ZFN_UTILS_LOCATION_CITY,
            WJH_UTILS_CITY_DATA,
            ZFN_UTILS_IOS_CITY_JSON,
            ZFN_UTILS_ANDROID_CITY_JSON,

            ZFN_UTILS_LOAN_SET,
            ZFN_UTILS_LOAN_CAL,
            ZFN_UTILS_LOAN_CAL_GROUP,
            ZFN_UTILS_AREA_DATA,
            ZFN_UTILS_KEY_MAP,
            ZFN_UTILS_ADCODE_POLINE,
            SYSTEM_HIDDEN
        ];

        if (in_array($interface, $ignoreList)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array|string
     */
    public function exec($interface)
    {
        switch ($interface) {
            //【1001】获取系统设置
            case WJH_UTILS_SETTINGS:
                return $this->settings();
            //【1002】获取短信验证码
            case ZFN_UTILS_SMS_CODE:
                return $this->smsCode();
            //【1003】安卓获取更新
            case ZFN_UTILS_APP_VERSION:
                return $this->appVersion();
            //【1004】苹果更新获取
            case ZFN_UTILS_APP_VERSION_IOS:
                return $this->appVersionIos();
            //【1005】定位解析城市
            case ZFN_UTILS_LOCATION_CITY:
                return $this->locationCity();
            //【1006】所有城市数据
            case WJH_UTILS_CITY_DATA:
                return $this->cityAll();
            //【1007】ios省市县json
            case ZFN_UTILS_IOS_CITY_JSON:
                return $this->iosCityJson();
            //【1008】android省市县json
            case ZFN_UTILS_ANDROID_CITY_JSON:
                return $this->androidCityJson();
            //【1009】贷款计算器参数
            case ZFN_UTILS_LOAN_SET:
                return $this->loanSet();
            //【1010】单一贷款计算
            case ZFN_UTILS_LOAN_CAL:
                return $this->loanCal();
            //【1011】组合贷款计算
            case ZFN_UTILS_LOAN_CAL_GROUP:
                return $this->loanCalGroup();
            //【1012】获取城市下区县
            case ZFN_UTILS_AREA_DATA:
                return $this->cityArea();
            //【1013】地点地图搜索
            case ZFN_UTILS_KEY_MAP:
                return $this->keyMap();
            //【1014】地图行政区域查询
            case ZFN_UTILS_ADCODE_POLINE:
                return $this->adcodePoline();
            //【1015】上架隐藏
            case SYSTEM_HIDDEN:
                return $this->system_hidden();
            default:
                send_developer_error("模块【工具】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    private function settings()
    {
        $data = UtilsModel::setting();
        $this->insertResponse('',$data);
        return $this->response('基础配置');
    }

    private function smsCode()
    {
        $user = $this->currentUser(false);
        $mobile = $this->essentialParam('mobile');
        $title = $this->optionalParam('title');
        $type = $this->essentialParam('type');
        $msg = Sms::sendSms($user,$mobile,$type,$title);
        return $this->response($msg);
    }


    private function appVersion()
    {
        //当前版本号
        $version = $this->essentialParam('version');
        $arr = AppVersion::checkVersion($version,1);
        $this->insertResponse('', $arr);
        return $this->response('获取最新版本成功！');
    }

    private function appVersionIos()
    {
        //当前版本号
        $version = $this->essentialParam('version');
        $arr = AppVersionIos::checkVersion($version,1);
        $this->insertResponse('', $arr);
        return $this->response('获取最新版本成功！');
    }

    private function locationCity()
    {
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $arr = Region::locationCity($longitude,$latitude);
        $this->insertResponse('', $arr);
        return $this->response('定位解析城市');
    }

    private function cityAll(){
        $arr = Region::getCityCache();
        foreach ($arr['city'] as &$item){
            unset($item['latitude']);
            unset($item['longitude']);
        }
        unset($item);
        $this->insertResponse('', $arr);
        return $this->response('所有城市');
    }

    private function iosCityJson(){
        $arr = Region::iosJson();
        $this->insertResponse('data', $arr);
        return $this->response('ios省市县json');
    }

    private function androidCityJson(){
        $arr = Region::androidJson();
        $this->insertResponse('data', $arr);
        return $this->response('android省市县json');
    }

    //统计下载来源
    private function downSrc()
    {
        $channel_no = $this->optionalParam('channel_no');
        //添加渠道
        $data = [
            "channel_no"=>$channel_no,
            "create_time"=>time(),
            "day_time"=>date('Ymd'),
            "month_time"=>date('Ym'),
            "year_time"=>date('Y')
        ];
        Db::name("channel_static")->insert($data);
        return $this->response('请求成功');
    }


    private function loanSet()
    {

        $arr = LoanCal::getSet();
        $this->insertResponse('', $arr);
        return $this->response('贷款计算器参数');
    }


    private function loanCal()
    {
        $money = $this->essentialParam('money');
        $rate = $this->essentialParam('rate');
        $num = $this->essentialParam('num');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $arr = LoanCal::calOne($money,$rate,$num,$longitude,$latitude);
        $this->insertResponse('', $arr);
        return $this->response('单一贷款计算');
    }

    private function loanCalGroup()
    {
        $money1 = $this->essentialParam('money1');
        $rate1 = $this->essentialParam('rate1');
        $num1 = $this->essentialParam('num1');

        $money2 = $this->essentialParam('money2');
        $rate2 = $this->essentialParam('rate2');
        $num2 = $this->essentialParam('num2');

        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $arr = LoanCal::calGroup($money1,$rate1,$num1,$money2,$rate2,$num2,$longitude,$latitude);
        $this->insertResponse('', $arr);
        return $this->response('组合贷款计算');
    }

    private function cityArea(){
        $city_id = $this->essentialParam('city_id');
        $arr = Region::getCityArea($city_id);
        $this->insertResponse('data', $arr);
        return $this->response('获取城市下区县');
    }

    private function keyMap(){
        $city_id = $this->essentialParam('city_id');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $keyword = $this->essentialParam('keyword');
        $data = UtilsModel::keyMapSearch($city_id,$longitude,$latitude,$keyword);
        $this->insertResponse('data', $data);
        return $this->response('地图关键词搜索');
    }

    private function adcodePoline(){
        $keywords = $this->essentialParam('keywords');
        $data = Region::adcodePoline($keywords);
        $this->insertResponse('', $data);
        return $this->response('地图行政区域查询');
    }
    private function system_hidden(){
        $data = Settings::getSystem('is_hidden');
        $this->insertResponse('', $data);
        return $this->response('获取成功');
    }
}